/**
 * 时间 yyyy-MM-dd hh:mm:ss
 * @param {*} time 2023-10-09T04:36:11.000+00:00
 */
function dateFunction(time) {
  var zoneDate = new Date(time).toJSON();
  var date = new Date(+new Date(zoneDate) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '');
  return date;
}

// 获取时间
function getCurrentToPreviousDateTimeRange(interval) {
  // 获取当前时间
  let currentDateTime = new Date();
  // 根据传入的间隔计算之前的时间
  let previousDateTime;
  switch (interval) {
    case 'week':
      previousDateTime = new Date(currentDateTime - 7 * 24 * 60 * 60 * 1000);
      break;
    case 'month':
      previousDateTime = new Date(currentDateTime.getFullYear(), currentDateTime.getMonth() - 1, currentDateTime.getDate());
      break;
    case 'halfYear':
      previousDateTime = new Date(currentDateTime.getFullYear(), currentDateTime.getMonth() - 6, currentDateTime.getDate());
      break;
    case 'year':
      previousDateTime = new Date(currentDateTime.getFullYear() - 1, currentDateTime.getMonth(), currentDateTime.getDate());
      break;
    default:
      throw new Error('Invalid interval');
  }

  // 格式化日期时间为YYYY-MM-DD HH:mm:ss格式
  function formatDateTime(dateTime) {
    let year = dateTime.getFullYear();
    let month = (1 + dateTime.getMonth()).toString().padStart(2, '0');
    let day = dateTime.getDate().toString().padStart(2, '0');
    let hours = dateTime.getHours().toString().padStart(2, '0');
    let minutes = dateTime.getMinutes().toString().padStart(2, '0');
    let seconds = dateTime.getSeconds().toString().padStart(2, '0');
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  }

  // 返回时间区间
  return {
    start: formatDateTime(currentDateTime),
    end: formatDateTime(previousDateTime)
  };
}
//获取字典中对应的值
function getDictNameForValue(list, value, attrVal = 'value', attrName = 'label') {
  const data = list.filter((obj) => {
    return obj[attrVal] === value || obj[attrVal] + '' === value + ''
  })
  if (data.length !== 0) {
    return data[0][attrName]
  }
  return ''
}
//格式化时间
function formatDateTime(date, format) {
  const o = {
    "M+": date.getMonth() + 1, // 月份
    "d+": date.getDate(), // 日
    "h+": date.getHours() % 12 === 0 ? 12 : date.getHours() % 12, // 小时
    "H+": date.getHours(), // 小时
    "m+": date.getMinutes(), // 分
    "s+": date.getSeconds(), // 秒
    "q+": Math.floor((date.getMonth() + 3) / 3), // 季度
    S: date.getMilliseconds(), // 毫秒
    a: date.getHours() < 12 ? "上午" : "下午", // 上午/下午
    A: date.getHours() < 12 ? "AM" : "PM", // AM/PM
  };
  if (/(y+)/.test(format)) {
    format = format.replace(
      RegExp.$1,
      (date.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  }
  for (let k in o) {
    if (new RegExp("(" + k + ")").test(format)) {
      format = format.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
      );
    }
  }
  return format;
}
//从树形结构中查找对应的值
function searchInTree(tree, treeKey, code) {
  for (let i = 0; i < tree.length; i++) {
    const node = tree[i];
    // 如果找到匹配的 areaCode，返回对应的 name
    if (node[treeKey] === code) {
      return node.name;
    }
    // 如果当前节点有子节点，递归查找
    if (node.children && node.children.length > 0) {
      const result = this.searchInTree(node.children,treeKey, code);
      if (result) {
        return result;
      }
    }
  }
  return ''; // 如果没有找到，返回 null
}

export default {
  dateFunction,
  getCurrentToPreviousDateTimeRange,
  getDictNameForValue,
  formatDateTime,
  searchInTree
}
